package com.hxrfid.web.controller.project;

import com.hxrfid.common.annotation.Log;
import com.hxrfid.common.annotation.RepeatSubmit;
import com.hxrfid.common.core.controller.BaseController;
import com.hxrfid.common.core.domain.PageQuery;
import com.hxrfid.common.core.domain.R;
import com.hxrfid.common.core.page.TableDataInfo;
import com.hxrfid.common.core.validate.AddGroup;
import com.hxrfid.common.core.validate.EditGroup;
import com.hxrfid.common.enums.BusinessType;
import com.hxrfid.common.utils.poi.ExcelUtil;
import com.hxrfid.project.domain.bo.BsSearchBo;
import com.hxrfid.project.domain.vo.BsSearchVo;
import com.hxrfid.project.service.IBsSearchService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 搜索历史
 *
 * @author hxrfid
 * @date 2025-06-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/project/search")
public class BsSearchController extends BaseController {

    private final IBsSearchService iBsSearchService;

    /**
     * 查询搜索历史列表
     */
    @GetMapping("/list")
    public TableDataInfo<BsSearchVo> list(BsSearchBo bo, PageQuery pageQuery) {
        return iBsSearchService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出搜索历史列表
     */
    @Log(title = "搜索历史", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BsSearchBo bo, HttpServletResponse response) {
        List<BsSearchVo> list = iBsSearchService.queryList(bo);
        ExcelUtil.exportExcel(list, "搜索历史", BsSearchVo.class, response);
    }

    /**
     * 获取搜索历史详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<BsSearchVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iBsSearchService.queryById(id));
    }

    /**
     * 新增搜索历史
     */
    @Log(title = "搜索历史", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BsSearchBo bo) {
        return toAjax(iBsSearchService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改搜索历史
     */
    @Log(title = "搜索历史", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BsSearchBo bo) {
        return toAjax(iBsSearchService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除搜索历史
     *
     * @param ids 主键串
     */
    @Log(title = "搜索历史", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iBsSearchService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
